programming4us
           
 
 
Programming

Programming WCF Services : Data Contracts - Collections (part 2) - The CollectionDataContract Attribute & Dictionaries

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
7/16/2011 4:04:59 PM

3. The CollectionDataContract Attribute

The mechanism shown so far for marshaling a concrete collection is suboptimal. For one thing, it requires the collection to be serializable and does not work with the service-oriented DataContract attribute. Also, while one party is dealing with a collection, the other is dealing with an array, and the two are not semantically equivalent: the collection is likely to offer some advantages, or it would not have been chosen in the first place. Furthermore, there is no compile-time or runtime verification of the presence of the Add() method or the IEnumerable and IEnumerable<T> interfaces, resulting in an unworkable data contract if they are missing. WCF’s solution is yet another dedicated attribute called CollectionDataContractAttribute, defined as:

[AttributeUsage(AttributeTargets.Struct|AttributeTargets.Class,Inherited = false)]
public sealed class CollectionDataContractAttribute : Attribute
{
public string Name
{get;set;}

public string Namespace
{get;set;}
//More members
}


The CollectionDataContract attribute is analogous to the DataContract attribute, and similarly, it does not make the collection serializable. When applied on a collection, the CollectionDataContract attribute exposes the collection to the client as a generic linked list. While the linked list may have nothing to do with the original collection, it does offer a more collection-like interface than an array.

For example, given this collection definition:

[CollectionDataContract(Name = "MyCollectionOf{0}")]
public class MyCollection<T> : IEnumerable<T>
{
public void Add(T item)
{}

IEnumerator<T> IEnumerable<T>.GetEnumerator()
{...}
//Rest of the implementation
}

and this service-side contract definition:

[ServiceContract]
interface IContactManager
{
[OperationContract]
void AddContact(Contact contact);

[OperationContract]
MyCollection<Contact> GetContacts();
}

the definitions the client ends up with after importing the metadata will be:

[CollectionDataContract]
public class MyCollectionOfContact : List<Contact>
{}

[ServiceContract]
interface IContactManager
{
[OperationContract]
void AddContact(Contact contact);

[OperationContract]
MyCollectionOfContact GetContacts();
}

In addition, at service load time the CollectionDataContract attribute verifies the presence of the Add() method as well as either IEnumerable or IEnumerable<T>. Failing to have these on the collection will result in an InvalidDataContractException.

Note that you cannot apply both the DataContract attribute and the CollectionDataContract attribute on a collection. Again, this is verified at service load time.

4. Referencing a Collection

WCF even lets you preserve the same collection on the client side as on the service side. The advanced settings dialog box for the service reference contains a Collection type combo box that lets you specify how to represent to the client certain kinds of collections and arrays found in the service metadata. For example, if the service operation returns one of the collections IEnumerable<T>, IList<T>, or ICollection<T>, by default the proxy will present it as an array (the default item in the combo box). However, you can request Visual Studio 2010 to use another collection, such as BindingList for data binding, a List<T>, Collection, or LinkedList<T>, and so on. If a conversion is possible, the proxy will use the requested collection type instead of an array, for example:

[OperationContract]
List<int> GetNumbers();

When you then define the collection in another assembly referenced by the client’s project, as I’ve discussed, that collection will be imported as-is. This feature is very useful when interacting with one of the built-in .NET collections, such as the Stack<T> collection defined in the System.dll, which is referenced by practically all .NET projects.

5. Dictionaries

A dictionary is a special type of collection that maps one data instance to another. As such, dictionaries do not marshal well either as arrays or as lists. Not surprisingly, dictionaries get their own representation in WCF.

If the dictionary is a serializable collection that supports the IDictionary interface, it will be exposed as a Dictionary<object,object>. For example, this service contract definition:

[Serializable]
public class MyDictionary : IDictionary
{...}

[ServiceContract]
interface IContactManager
{
...
[OperationContract]
MyDictionary GetContacts();
}

will be exposed as this definition:

[ServiceContract]
interface IContactManager
{
...
[OperationContract]
Dictionary<object,object> GetContacts();
}

This, by the way, includes using the HashTable collection.

If the serializable collection supports the IDictionary<K,T> interface, as in:

[Serializable]
public class MyDictionary<K,T> : IDictionary<K,T>
{...}

[ServiceContract]
interface IContactManager
{
...
[OperationContract]
MyDictionary<int,Contact> GetContacts();
}

the exported representation will be as a Dictionary<K,T>:

[ServiceContract]
interface IContactManager
{
...
[OperationContract]
Dictionary<int,Contact> GetContacts();
}

This includes making direct use of Dictionary<K,T> in the original definition, instead of MyDictionary<K,T>.

If instead of merely being a serializable collection, the dictionary is decorated with the CollectionDataContract attribute, it will be marshaled as a subclass of the respective representation. For example, this service contract definition:

[CollectionDataContract]
public class MyDictionary : IDictionary
{...}

[ServiceContract]
interface IContactManager
{
...
[OperationContract]
MyDictionary GetContacts();
}

will have this representation:

[CollectionDataContract]
public class MyDictionary : Dictionary<object,object>
{}

[ServiceContract]
interface IContactManager
{
...
[OperationContract]
MyDictionary GetContacts();
}

while this generic collection:

[CollectionDataContract(Name = "MyDictionary")]
public class MyDictionary<K,T> : IDictionary<K,T>
{...}

[ServiceContract]
interface IContactManager
{
...
[OperationContract]
MyDictionary<int,Contact> GetContacts();
}

will be published in the metadata as:

[CollectionDataContract]
public class MyDictionary : Dictionary<int,Contact>
{}

[ServiceContract]
interface IContactManager
{
...
[OperationContract]
MyDictionary GetContacts();
}

As for a collection, in the advanced settings dialog box for a service reference, you can request other dictionary types, such as the SortedDictionary<T,K>, HashTable, or ListDictionary type, and the proxy will use that dictionary instead if possible.
Other -----------------
- Programming WCF Services : Data Contracts - Collections (part 1) - Concrete Collections & Custom Collections
- iPhone Programming : The Image Picker View Controller - Adding the Image Picker to the City Guide Application
- iPhone Programming : Other View Controllers - Modal View Controllers
- jQuery 1.3 : DOM Manipulation - Inserting new elements
- jQuery 1.3 : DOM Manipulation - Manipulating attributes
- DirectX 10 Game Programming : Adding the DirectX Libraries
- jQuery 1.3 : AJAX - Additional options
- jQuery 1.3 : AJAX and events & Security limitations
- jQuery 1.3 : AJAX - Keeping an eye on the request
- jQuery 1.3 : AJAX - Passing data to the server
- iPhone Programming : Other View Controllers - Tab Bar Applications
- iPhone Programming : Other View Controllers - Utility Applications
- iPhone Programming : Table-View-Based Applications - Adding a City View
- iPhone Programming : Table-View-Based Applications - Adding Navigation Controls to the Application
- iPad SDK : Popovers - Popover Preparations
- iPad SDK : Preparing Dudel for a New Tool (part 5) - Rendering Multiple Styles
- iPad SDK : Preparing Dudel for a New Tool (part 4) - Creating a New Drawable Class
- jQuery 1.3 : AJAX - Loading data on demand (part 3) - Loading an XML document
- jQuery 1.3 : AJAX - Loading data on demand (part 2) - Working with JavaScript objects
- jQuery 1.3 : AJAX - Loading data on demand (part 1) - Appending HTML
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us